package com.mx.nbainformation.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.mx.nbainformation.R;
import com.mx.nbainformation.Model.DayData;
import com.mx.nbainformation.Model.MatchData;
import com.mx.nbainformation.base.BaseActivity;
import com.mx.nbainformation.biz.Biz_Schedule;
import com.mx.nbainformation.config.Constants;
import com.umeng.analytics.MobclickAgent;

public class ActivityScheduleDay extends BaseActivity implements
		OnItemClickListener {

	private int _id = 0;
	private Biz_Schedule _bizSchedule;
	private List<Map<String, Object>> _list;
	private SimpleAdapter _adapter;

	// view
	private ListView _lvMatch;
	private TextView _txtSecondTitle;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_schedule_day);
		TitleButtonOfOne(false);
		initClass();
		initNoItems();
		initControl();
		initList();
		addZhuamob();

	}

	private void initControl() {
		_lvMatch = (ListView) findViewById(R.id.listScheduleDay);
		_txtSecondTitle = (TextView) findViewById(R.id.txtScheduleDayTitle);
	}

	private void initList() {
		_list = new ArrayList<Map<String, Object>>();
		_adapter = new SimpleAdapter(ActivityScheduleDay.this, _list,
				R.layout.item_schedule_day, new String[] { MatchData.STATE,
						MatchData.TYPE, MatchData.MAIN, MatchData.SCORE,
						MatchData.VISITING }, new int[] { R.id.item_day_state,
						R.id.item_day_type, R.id.item_day_main,
						R.id.item_day_score, R.id.item_day_visiting });
		_lvMatch.setAdapter(_adapter);
		_lvMatch.setOnItemClickListener(this);
	}

	private void initClass() {
		_bizSchedule = new Biz_Schedule(ActivityScheduleDay.this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Intent intent = getIntent();
		_id = intent.getIntExtra(Constants.ScheduleId, 0);
		new DataAsyncTask().execute();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	class DataAsyncTask extends AsyncTask<Void, Void, DayData<MatchData>> {

		@Override
		protected DayData<MatchData> doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return _bizSchedule.getDayDataById(_id);
		}

		@Override
		protected void onPostExecute(DayData<MatchData> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				_noItem.setVisibility(8);
				_list.clear();
				_txtSecondTitle.setText(result.get_date());
				for (int i = 0; i < result.get_listMatch().size(); i++) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put(MatchData.STATE, result.get_listMatch().get(i)
							.get_state());
					map.put(MatchData.TYPE, result.get_listMatch().get(i)
							.get_type());
					map.put(MatchData.MAIN, result.get_listMatch().get(i)
							.get_main());
					map.put(MatchData.SCORE, result.get_listMatch().get(i)
							.get_score());
					map.put(MatchData.VISITING, result.get_listMatch().get(i)
							.get_visiting());
					map.put(MatchData.TARGETURL, result.get_listMatch().get(i)
							.get_targetUrl());
					_list.add(map);
				}
				_adapter.notifyDataSetChanged();
			} else {
				_noItem.setVisibility(0);
			}
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		try {
			MobclickAgent.onEvent(this, "scheduleDay", "itemClick");
			HashMap<String, Object> hash = (HashMap<String, Object>) _lvMatch
					.getItemAtPosition(position);
			Intent intent=new Intent();
			intent.setClass(ActivityScheduleDay.this, ActivityPlayerScore.class);
			intent.putExtra(MatchData.TARGETURL, hash.get(MatchData.TARGETURL).toString());
			startActivity(intent);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
